<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/23
 * Time: 10:24
 */
namespace App\Service\Admin;

use App\Http\Model\Mysql\SupplyModel;
use Carbon\Carbon;

class SupplyService extends BaseService
{
    /**
     * 供应商列表
     * @param array $query 搜索条件
     * @param int $page 当前页码
     * @param int $size 每页数量
     * @return array
     */
    public function supplyList(array $query = [], int $page = 1, int $size = 0)
    {
        $where['is_delete'] =   SupplyModel::NOT_DELETE;
        if (isset($query['supply_name']) && !empty($query['supply_name'])) {
            $where[]   =   ['supply_name', 'like', '%' . trim($query['supply_name']) . '%'];
        }

        $size   =   $size <= 0 ? self::PAGE_LIMIT : $size;
        $count  =   SupplyModel::where($where)->count();

        $list   =   SupplyModel::where($where)->limit($size)->offset(($page - 1) * $size)->orderBy('sorting', 'desc')->select(['id', 'supply_name', 'supply_desc', 'sorting', 'unique_id', 'updated_at'])
                    ->get()->each(function (&$item) {
                        $item->update_time  =   $item->updated_at->toDateTimeString();
                        unset($item->updated_at);
                    });

        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'pages' =>  ceil($count / $size),
                'list'  =>  $list->toArray(),
                'current'=> $page
            ]];
        }
    }

    /**
     * 供应商总列表页
     * @return array
     */
    public function supplyTotal()
    {
        $where['is_delete'] =   SupplyModel::NOT_DELETE;

        $count  =   SupplyModel::where($where)->count();
        $list   =   SupplyModel::where($where)->orderBy('sorting', 'desc')->select(['id', 'supply_name', 'supply_desc', 'sorting', 'unique_id', 'updated_at'])
                    ->get()->each(function (&$item) {
                        $item->update_time  =   $item->updated_at->toDateTimeString();
                        unset($item->updated_at);
                    });

        if ($list->isEmpty()) {
            return [true, []];
        } else {
            return [true, [
                'count' =>  $count,
                'pages' =>  1,
                'list'  =>  $list->toArray(),
                'current'=> 1
            ]];
        }
    }

    /**
     * 供应商删除
     * @param int $supply_id 供应商ID
     * @return array
     */
    public function supplyDelete(int $supply_id)
    {
        $delete_result  =   SupplyModel::where(['id' => $supply_id])->update(['is_delete' => SupplyModel::IS_DELETE, 'delete_time' => Carbon::now()->timestamp]);
        if ($delete_result) {
            return [true, '删除成功'];
        } else {
            return [false, '删除失败'];
        }
    }

    /**
     * 供应商添加
     * @param array $params 参数
     * @return array
     */
    public function supplyAdd(array $params)
    {
        $insert['supply_name']  =   $params['supply_name'];
        $insert['supply_desc']  =   $params['supply_desc'] ?? '';
        $insert['sorting']      =   $params['sorting'];
        $insert['unique_id']    =   createUniqueID();
        $insert['is_delete']    =   SupplyModel::NOT_DELETE;

        $insert_result  =   SupplyModel::create($insert);
        if ($insert_result) {
            return [true, '添加成功'];
        } else {
            return [false, '添加失败'];
        }
    }

    /**
     * 供应商修改
     * @param array $params  参数内容
     * @param int $supply_id 供应商ID
     * @return array
     */
    public function supplyEdit(array $params, int $supply_id)
    {
        $update['supply_name']  =   $params['supply_name'];
        $update['supply_desc']  =   $params['supply_desc'] ?? '';
        $update['sorting']      =   $params['sorting'] ?? 0;

        $update_result  =   SupplyModel::where(['id' => $supply_id])->update($update);

        if ($update_result) {
            return [true, '修改成功'];
        } else {
            return [false, '修改失败'];
        }
    }
}
